#include <iostream>
#include <string>
#include "MprpcApplication.h"
#include "MprpcController.h"
#include "RpcProvider.h"
#include "user.pb.h"

class UserService : public fixbug::UserServiceRpc
{
public:
    bool Login(std::string name, std::string pwd)
    {
        std::cout << "doing local service: Login" << std::endl;
        std::cout << "name: " << name << "   pwd:" << pwd << std::endl;
        return true;
    }
    bool Register(uint32_t id, std::string name, std::string pwd)
    {
        std::cout << "doing local service: Register" << std::endl;
        std::cout << "id: " << id << "name: " << name << "   pwd:" << pwd << std::endl;
        return true;
    }

    virtual void Login(::google::protobuf::RpcController *controller,
                       const ::fixbug::LoginRequest *request,
                       ::fixbug::LoginResponse *response,
                       ::google::protobuf::Closure *done)
    {
        std::string name = request->name();
        std::string pwd = request->pwd();
        fixbug::ResultCode *code = response->mutable_result();
        if (Login(name, pwd))
        {
            response->set_success(true);
            code->set_errcode(0);
            code->set_errmsg("");
        }
        else
        {
            response->set_success(false);
            code->set_errcode(1);
            code->set_errmsg("error");
        }
        if(done)
            done->Run();
    }
    virtual void Register(::google::protobuf::RpcController *controller,
                          const ::fixbug::RegisterRequest *request,
                          ::fixbug::RegisterResponse *response,
                          ::google::protobuf::Closure *done)
    {
        std::string name = request->name();
        std::string pwd = request->pwd();
        uint32_t id = request->id();
        fixbug::ResultCode *code = response->mutable_result();
        if (Register(id, name, pwd))
        {
            // controller->SetFailed("wodewodewodewode");  演示controller的作用的代码
            response->set_success(true);
            code->set_errcode(0);
            code->set_errmsg("");
        }
        else
        {
            response->set_success(false);
            code->set_errcode(1);
            code->set_errmsg("error");
        }
        if(done)
            done->Run();
    };
};

int main(int argc, char **argv)
{
    MprpcApplication::Init(argc, argv);

    RpcProvider provider;
    provider.NotifyService(new UserService());

    provider.Run();

    return 0;
}
